﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SQLite;
using System.Data.Common;

namespace NokiaContactsExtractor
{
  public  class Extractor
    {
       string dbPath;

      public Extractor(string sqlitePath)
      {
          dbPath = sqlitePath;
         
   
      }

      public List<Contact> GetContacts(){

          List<Contact> contacts = new List<Contact>();

         // StreamWriter writer = null;
          try
          {
              SQLiteFactory factory = (SQLiteFactory)DbProviderFactories.GetFactory("System.Data.SQLite");
              using (SQLiteConnection connection = (SQLiteConnection)factory.CreateConnection())
              {
                  connection.ConnectionString = string.Format("Data Source={0};Version=3;New=False;Compress=True;",dbPath);
                  connection.Open();
                  Console.WriteLine("Connection opened..");
                  using (SQLiteCommand command = new SQLiteCommand(connection))
                  {
                      command.CommandText = @"SELECT * FROM Contact;";
                      var reader = command.ExecuteReader();
                     // FileInfo outF = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "result" + DateTime.Now.Date.ToString("dd_mm_yyyy") + ".txt"));
                    //  writer = new StreamWriter(outF.Open(FileMode.Create));
                    //  writer.WriteLine("Sender, Text, Timestamp, Created");
                      while (reader.Read())
                      {
                            Contact newContact=new Contact();
                         
                              var colValues=reader.GetValues();
                              newContact.Email = colValues["GeneralEmail"];
                              newContact.FirstName = colValues["GivenName"];
                              newContact.LastName = colValues["LastName"];
                            

                            List<string> phoneNums=new List<string>();
                           if(!string.IsNullOrEmpty(colValues["GeneralMobile"]))  phoneNums.Add(colValues["GeneralMobile"]);
                           if(!string.IsNullOrEmpty(colValues["GeneralMobile2"]))  phoneNums.Add(colValues["GeneralMobile2"]);
                           if(!string.IsNullOrEmpty(colValues["GeneralMobile3"]))  phoneNums.Add(colValues["GeneralMobile3"]);
                            if(!string.IsNullOrEmpty(colValues["GeneralPhone"]))  phoneNums.Add(colValues["GeneralPhone"]);

                             newContact.Email = colValues["GeneralEmail"];
                             newContact.Web = colValues["WWW"];
                             newContact.PhoneNumbers = phoneNums.ToArray();
                            //Guid 0
                            //GivenName 2
                            //MiddleName 3
                            //GeneralMobile 10
                            //GeneralMobile2 11
                            //GeneralMobile3 12
                            //GeneralPhone :  : 13

                            //GeneralEmail : 16
                             contacts.Add(newContact);
                      }
                      Console.WriteLine("Done! Press eny Key to continue.");
                  }
              }
       
              return contacts;
          }
          catch (Exception ex)
          {
              Console.WriteLine("OOPS:" + ex.Message);
              return null;
          }
          finally
          {
             /// if (writer != null)
                //  writer.Close();
          }

      }


      public void GetMessages()
      {
       

          StreamWriter writer = null;
          try
          {
              SQLiteFactory factory = (SQLiteFactory)DbProviderFactories.GetFactory("System.Data.SQLite");
              using (SQLiteConnection connection = (SQLiteConnection)factory.CreateConnection())
              {
                  connection.ConnectionString = "Data Source = " + dbPath;
                  connection.Open();
                  Console.WriteLine("Connection opened..");
                  using (SQLiteCommand command = new SQLiteCommand(connection))
                  {
                      command.CommandText = @"SELECT MessageEx.Sender, MessageEx.Text, MessageEx.SentReceivedTimestamp, Item.CreationDate FROM MessageEx INNER JOIN Item ON MessageEx.GUID = Item.GUID ORDER BY MessageEx.SentReceivedTimestamp;";
                      var reader = command.ExecuteReader();
                      FileInfo outF = new FileInfo("result" + DateTime.Now.Date.ToString("dd_mm_yyyy") + ".txt");
                      writer = new StreamWriter(outF.Open(FileMode.Create));
                      writer.WriteLine("Sender, Text, Timestamp, Created");
                      while (reader.Read())
                      {
                          writer.WriteLine(String.Format("{0}, {1}, {2}, {3}", reader.GetString(0), reader.GetString(1), reader.GetDouble(2), reader.GetDouble(3)));
                          Console.Write(".");
                      }
                      Console.WriteLine("Done! Press eny Key to continue.");
                  }
              }
          }
          catch (Exception ex)
          {
              Console.WriteLine("OOPS:" + ex.Message);
             
          }
          finally
          {
              if (writer != null)
                  writer.Close();
          }

      }


    }
}
